package com.bootdo.order.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.merchant.domain.AgentDO;
import com.bootdo.merchant.domain.MerchantDO;
import com.bootdo.order.domain.OrderPayDO;
import com.bootdo.order.service.OrderMercService;

/**
 * @Desc 订单管理(商户订单管理)
 *              提供给后台管理平台使用
 * @date 2019/12/2
 *
 */
@Controller
@RequestMapping("/orders/merchant")
public class OrderMercController {

    private final String PREFIX = "orders/merchant";
    @Autowired
    private OrderMercService orderMercService;

    @GetMapping()
    @RequiresPermissions("orders:merchant:order")
    String order(Model model) {
        return PREFIX + "/list";
    }

    @ResponseBody
    @PostMapping("/findAgent")
    @RequiresPermissions("orders:merchant:order")
    public R findAgent(){
        List<AgentDO> list = orderMercService.findAgent();
        return R.ok(list);
    }

    @ResponseBody
    @PostMapping("/findMerchant")
    @RequiresPermissions("orders:merchant:order")
    public R findMerchant(Long userId){
        List<MerchantDO> list = orderMercService.findMerchant(userId);
        return R.ok(list);
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("orders:merchant:order")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<OrderPayDO> userList = orderMercService.list(query);
        int total = orderMercService.count(query);
        PageUtils pageUtils = new PageUtils(userList, total);
        return pageUtils;
    }

    @Log("手动回调商户")
    @ResponseBody
    @PostMapping("/callback")
    @RequiresPermissions("orders:merchant:edit")
    R callbackMerchant(Long id){
        try {
            orderMercService.callbackMerchant(id);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}
